package com.ta.controller;

import com.ta.mapper.ProductMapper;
import com.ta.pojo.Order;
import com.ta.pojo.Product;
import com.ta.pojo.vo.OrderVO;
import com.ta.service.OrderService;
import com.ta.service.ProductService;
import com.ta.service.ex.OrderException;
import com.ta.util.JsonResult;
import org.apache.tomcat.util.http.parser.HttpParser;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController extends BaseController{
    @Autowired
    private OrderService orderService;
    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    @Value("${delay.exchange}")
    private String delayExchange;
    @Value("${delay.routingKey}")
    private String delayRoutingKey;

    @Value("${order.exchange}")
    private String orderExchange;
    @Value("${order.routingKey}")
    private String orderRoutingKey;

    /**
     * 创建订单请求
     * @return
     */
    @RequestMapping("creat_order")
    public JsonResult<Order> insert(Integer[] cids,
                                    Integer aid,
                                    HttpSession session){
        Order order = orderService.insert(getUid(session), aid, cids, getUserName(session));

        /*将订单加到延迟队列*/
        rabbitTemplate.convertAndSend(delayExchange, delayRoutingKey, order.getOid(), message ->{
            message.getMessageProperties().setExpiration("10000");
            return message;
        });

        return new JsonResult<>(200);
    }

    /**
     * 在线付款请求
     * @param session
     * @return
     */
    @RequestMapping("select_ord_totalPrice")
    public JsonResult<Order> getOidAndTotalPrice(Integer oid, HttpSession session, Integer pid){
        /*使用rabbitmq队列完成支付订单*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("oid",oid);
        map.put("status",1);
        map.put("userName",getUserName(session));
        map.put("pid",pid);
        rabbitTemplate.convertAndSend(orderExchange,orderRoutingKey, map);
        return new JsonResult<>(200);
    }

    /**
     * 我的订单请求
     * @param session
     * @return
     */
    @RequestMapping("select_orderList")
    public JsonResult<List<OrderVO>> selectList(HttpSession session){
        List<OrderVO> orderVOList = orderService.selectOrderList(getUid(session));
        return new JsonResult<>(200, orderVOList);
    }

    /**
     * 未收货订单请求
     * @return
     */
    @RequestMapping("select_noConfirm")
    public JsonResult<List<OrderVO>> selectNoConfirm(){
        List<OrderVO> orderVOList = orderService.selectNoConfirm();
        return new JsonResult<>(200, orderVOList);
    }

    /**
     * 未付款订单请求
     * @return
     */
    @RequestMapping("select_noPayment")
    public JsonResult<List<OrderVO>> selectNoPayment(){
        List<OrderVO> orderVOList = orderService.selectNoPayment();
        return new JsonResult<>(200, orderVOList);
    }

    /**
     * 确认收货请求
     * @return
     */
    @RequestMapping("confirm_signFor")
    public JsonResult<Void> confirmSignFor(Integer oid, HttpSession session){
        orderService.modifyStatus(oid, 1, 4, getUserName(session));
        return new JsonResult<>(200);
    }

    /**
     * 未支付请求支付
     * @return
     */
    @RequestMapping("no_payment")
    public JsonResult<Void> noNoPayment(Integer oid, HttpSession session){
        Order order = orderService.selectOid(oid);

        session.setAttribute("oid",oid);
        session.setAttribute("totalPrice",order.getTotalPrice());

        return new JsonResult<>(200);
    }

    /**
     * 订单详情请求
     * @param id
     * @return
     */
    @RequestMapping("find_orderInfo")
    public JsonResult<OrderVO> selectOrderInfo(Integer id, HttpSession session){
        OrderVO orderVO = orderService.selectOrderInfo(id, getUid(session));
        return new JsonResult<>(200, orderVO);
    }
}
